package hang.xipujewelWeb.entity.account;

import hang.xipujewelWeb.entity.IdEntity;
import hang.xipujewelWeb.entity.app.Order;
import hang.xipujewelWeb.entity.app.PriceBaseNumber;
import hang.xipujewelWeb.entity.app.ShopAddress;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "xipu_user")
public class User extends IdEntity {
	private String loginName;
	//	private String name;
	private String plainPassword;
	private String password;
	private String sourcePassword;
	private String salt;
	private Date registerDate;
	private int type; //0管理员,1 代理商,2 二级零售，3 直属零售
	private ShopAddress shopAddress;//收货地址
	private List<User> secondUsers = new ArrayList<User>();//部分代理商有可能发展自己的零售
	private User parent;//如果是零售商，这个对象就是其代理商
	private PriceBaseNumber priceBaseNumber;//该用户如果是厂商或者代理商，那么则拥有批发价基数，用于保存每次修改的批发价后的倍数
	private List<Order> orders = new ArrayList<Order>();

	public User() {
	}

	public User(Long id) {
		this.id = id;
	}

	@NotBlank
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	//	@NotBlank
	//	public String getName() {
	//		return name;
	//	}
	//
	//	public void setName(String name) {
	//		this.name = name;
	//	}

	// 不持久化到数据库，也不显示在Restful接口的属性.
	@Transient
	@JsonIgnore
	public String getPlainPassword() {
		return plainPassword;
	}

	public void setPlainPassword(String plainPassword) {
		this.plainPassword = plainPassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	// 设定JSON序列化时的日期格式
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "SHOP_ADRESS_ID")
	public ShopAddress getShopAddress() {
		return shopAddress;
	}

	public void setShopAddress(ShopAddress shopAddress) {
		this.shopAddress = shopAddress;
	}

	@ManyToOne(cascade = CascadeType.REFRESH)
	@JoinColumn(name = "PARENT_ID")
	public User getParent() {
		return parent;
	}

	public void setParent(User parent) {
		this.parent = parent;
	}

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
	public List<User> getSecondUsers() {
		return secondUsers;
	}

	public void setSecondUsers(List<User> secondUsers) {
		this.secondUsers = secondUsers;
	}

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	@OneToOne(cascade = { CascadeType.REMOVE, CascadeType.PERSIST })
	@JoinColumn(name = "PRICE_BASE_NUMBER")
	public PriceBaseNumber getPriceBaseNumber() {
		return priceBaseNumber;
	}

	public void setPriceBaseNumber(PriceBaseNumber priceBaseNumber) {
		this.priceBaseNumber = priceBaseNumber;
	}

	public String getSourcePassword() {
		return sourcePassword;
	}

	public void setSourcePassword(String sourcePassword) {
		this.sourcePassword = sourcePassword;
	}

}